<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>学生信息管理</title>
  <link rel="stylesheet" href="css/index.css" />
</head>

<body>
  <h1>新增学员</h1>
  <form class="info" autocomplete="off">
    姓名：<input type="text" class="uname" name="uname" />
    年龄：<input type="text" class="age" name="age" />
    性别:
    <select name="gender" class="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资：<input type="text" class="salary" name="salary" />
    就业城市：<select name="city" class="city">
      <option value="北京">北京</option>
      <option value="上海">上海</option>
      <option value="广州">广州</option>
      <option value="深圳">深圳</option>
      <option value="曹县">曹县</option>
    </select>
    <button class="add">录入</button>
  </form>

  <h1>就业榜</h1>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <!-- 
        <tr>
          <td>1001</td>
          <td>欧阳霸天</td>
          <td>19</td>
          <td>男</td>
          <td>15000</td>
          <td>上海</td>
          <td>
            <a href="javascript:">删除</a>
          </td>
        </tr> 
        -->
    </tbody>
  </table>
  <script>

    const add = document.querySelector('.add')
    const tbody = document.querySelector('tbody')


    function name(stu) {
      tbody.innerHTML = ''
      for (let i = 0; i < stu.length; i++) {
        //创建节点
        const tr = document.createElement('tr')
        tr.innerHTML = `
            <td>${stu[i].id}</td>
            <td>${stu[i].uname}</td>
            <td>${stu[i].age}</td>
            <td>${stu[i].gender}</td>
            <td>${stu[i].salary}</td>
            <td>${stu[i].city}</td>
            <td>
              <a href="javascript:" data-id="${stu[i].id}">删除</a>
            </td>
    `
        //追加节点
        tbody.appendChild(tr)
      }
    }
    //渲染
    let stu = JSON.parse(localStorage.getItem('stu')) || []
    name(stu)

    add.addEventListener('click', function (e) {
      //阻止默认行为
      e.preventDefault()

      const uname = document.querySelector('.uname').value//姓名
      const age = document.querySelector('.age').value//年龄
      const gender = document.querySelector('.gender').value//性别
      const salary = document.querySelector('.salary').value//薪资
      const city = document.querySelector('.city').value//就业城市

      //id
      let id = 0
      if (stu.length > 0) {
        id = stu[stu.length - 1].id + 1
      }
      else {
        id = 1
      }

      //存入对象
      let user = {}
      user.id = id
      user.uname = uname
      user.age = age
      user.salary = salary
      user.gender = gender
      user.city = city
      stu.push(user)

      //存入本地存储
      localStorage.setItem('stu', JSON.stringify(stu))

      //刷新
      location.reload()
    })

    //删除
    tbody.addEventListener('click', function (e) {
      if (e.target.tagName === 'A') {
        for (let i = 0; i < stu.length; i++) {
          if (stu[i].id === +e.target.dataset.id) {
            stu.splice(i, 1)
          }
        }
      }

      //更新本地存储
      localStorage.setItem('stu', JSON.stringify(stu))

      //刷新
      location.reload()
    })
  </script>

</body>

</html>